﻿using MySqlX.XDevAPI.Common;
using Quartz;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using XiAnAirport.BUS;
using XiAnAirport.BUS.RocketMQ;
using XiAnAirport.DAL;
using XiAnAirport.ElevatorWebSocket;
using XiAnAirport.Models;

namespace XiAnAirport.Job
{
    public class RunJob : IJob
    {
        public async Task Execute(IJobExecutionContext context)
        {
            //UpdateElevatorNum();
            UpdateRunTimeData();
            UpdateRunsData();
            UpdateOpenCount();
        }

        public static void UpdateRunTimeData()
        {
            ElevatorAreaDAL da = new ElevatorAreaDAL();
            try
            {

                List<ElevatorStateRunTime> list = new List<ElevatorStateRunTime>();
                list = ElevatorStateBUS.runtime_list;
                if (list != null)
                {
                    for (int k = 0; k < list.Count; k++)
                    {
                        ElevatorStateRunTime elevatorStateRun = new ElevatorStateRunTime();
                        elevatorStateRun = list[k];
                        DateTime dt = elevatorStateRun.runDate;

                        int data = da.QueryRunTime(elevatorStateRun.elevatorId, dt.Year, dt.Month, dt.Day);
                        if (data > 0)
                        {
                            string result = string.Empty;
                            result = da.UpdateRunTime(elevatorStateRun.elevatorId, elevatorStateRun.runTime, dt.ToString("yyyy-MM-dd"));
                            if (result == "success")
                            {
                                ElevatorStateBUS elevatorStateBUS = new ElevatorStateBUS();
                                elevatorStateBUS.RemoveRunTimeData(elevatorStateRun.elevatorId, dt);
                            }
                        }
                        else
                        {
                            da.InsertRunTime(elevatorStateRun.elevatorId, elevatorStateRun.runTime, dt.ToString("yyyy-MM-dd"));
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        public static void UpdateRunsData()
        {
            try
            {
                ElevatorAreaDAL da = new ElevatorAreaDAL();
                List<ElevatorStateRun> list_runs = new List<ElevatorStateRun>();
                list_runs = ElevatorStateBUS.run_list;
                if (list_runs != null)
                {
                    for (int i = 0; i < list_runs.Count; i++)
                    {
                        ElevatorStateRun elevatorStateRun = new ElevatorStateRun();
                        elevatorStateRun = list_runs[i];
                        DateTime dt = Convert.ToDateTime(elevatorStateRun.runsDate);

                        int data = da.QueryRuns(elevatorStateRun.elevatorId, dt.Year, dt.Month, dt.Day);
                        if (data > 0)
                        {
                            string result = string.Empty;

                            result = da.UpdateRuns(elevatorStateRun.elevatorId, elevatorStateRun.runs, dt.ToString("yyyy-MM-dd"));
                            if (result == "success" )
                            {
                                ElevatorStateBUS elevatorStateBUS = new ElevatorStateBUS();
                                elevatorStateBUS.RemoveRunsData(elevatorStateRun.elevatorId, dt);
                            }
                        }
                        else
                        {
                            da.InsertRuns(elevatorStateRun.elevatorId, elevatorStateRun.runs, dt.ToString("yyyy-MM-dd"));
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        public static void UpdateElevatorNum()
        {
            try
            {
                KoneDeviceStatus koneDeviceStatus = ElevatorStateBUS.KoneDeviceStatus;
                MqttDataDAL mqttDataDAL = new MqttDataDAL();
                for (int i = 0; i < 5; i++)
                {
                    try
                    {
                        string type = string.Empty;
                        int datanum = 0;
                        mqttdata mqttdata = new mqttdata();

                        if (i == 0)
                        {
                            type = "normalTotal";
                            datanum = koneDeviceStatus.normalTotal;
                        }
                        else if (i == 1)
                        {
                            type = "faultTotal";
                            datanum = koneDeviceStatus.faultTotal;
                        }
                        else if (i == 2)
                        {
                            type = "maintenanceTotal";
                            datanum = koneDeviceStatus.maintenanceTotal;
                        }
                        else if (i == 3)
                        {
                            type = "offlineTotal";
                            datanum = koneDeviceStatus.offlineTotal;
                        }
                        else if (i == 4)
                        {
                            type = "deviceTotal";
                            datanum = koneDeviceStatus.deviceTotal;
                        }
                        mqttdata.dataNum = datanum;
                        mqttdata.manufacturer = "KONE";
                        mqttdata.manufacturerId = "04";
                        mqttdata.topicType = "DeviceStatusTotal";
                        mqttdata.dataType = type;
                        bool result = mqttDataDAL.CheckData(mqttdata.manufacturer, mqttdata.manufacturerId, type, mqttdata.topicType);

                        if (result)
                        {
                            mqttDataDAL.UpdateMqttData(mqttdata);
                        }
                        else
                        {
                            mqttDataDAL.AddMqttData(mqttdata);
                        }
                    }
                    catch
                    {
                        continue;
                    }
                }
            }
            catch
            {

            }
        }

        public void UpdateOpenCount()
        {
            ElevatorAreaDAL da = new ElevatorAreaDAL();
            try
            {
                List<LiftStateALL> state_list = ElevatorStateBUS.state_list;
                for (int i = 0; i < state_list.Count; i++)
                {
                    da.UpdateOpenCount(state_list[i].opcNO, state_list[i].OpenTime, state_list[i].bend, state_list[i].RunTimeSecondSum);
                }
            }
            catch
            {

            }
        }
    }

}
